import Vue from 'vue'
import VueRouter from 'vue-router'
// import layout from '../views/layout.vue'
// import register from '../views/register.vue'
// import login from '../views/login.vue'
// import detail from '../views/detail.vue'
// import article from '../views/article.vue'
// import collect from '../views/collect.vue'
// import like from '../views/like.vue'
// import user from '../views/user.vue'
import { getToken } from '@/utils/storage'
import { Toast } from 'vant'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: () => import('../views/layout.vue'),
    redirect: 'article',
    children: [
      { path: '/article', component: () => import('../views/article.vue') },
      { path: '/collect', component: () => import('../views/collect.vue') },
      { path: '/like', component: () => import('../views/like.vue') },
      { path: '/user', component: () => import('../views/user.vue') }
    ]
  },
  { path: '/register', component: () => import('../views/register.vue') },
  { path: '/login', component: () => import('../views/login.vue') },
  { path: '/detail/:id', component: () => import('../views/detail.vue') }
]

const router = new VueRouter({
  routes
})

// 页面拦截
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    const whiteList = ['/login', '/register']
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      Toast.fail('请先登录')
    }
  }
})

export default router
